//
// there is no "newline" regular express in source insight!
// so si can't deal with "newline"
//

//------------------------------------
// test code and data
//

// aaaabb
// aabbbb

//hbuf=GetCurrentBuf()
//sch="\\(aa\\)\\(bb\\)"
//DbgMsg("sch",sch)
//hbuf1=NewBuf("")
//SearchInBufEx(hbuf, sch, 0, 0, 0, 1 0, hbuf1, 1)
//newWnd(hbuf1)

//Select_Char_Right
// find starting pattern
//stop


// para: the same with SearchInBuf expect hRstBuf and fSel
//		hRstBuf: the search result each line for a group, line 0 for all
// 		fSel: if set the search result selected
// return : the same with SearchInBuf
macro SearchInBufEx(hbuf, pattern, lnStart, ichStart, fMatchCase, fRegExp, fWholeWordsOnly, hRstBuf, fSel)
{
	searchRst=SearchInBuf(hbuf, pattern, lnStart, ichStart, fMatchCase, fRegExp, fWholeWordsOnly)
//DbgMsg("searchRst",searchRst)
	if( (1!=fRegExp) || (""==searchRst) ||(""==hRstBuf)){
		return searchRst
	}

	// get the regular express group number
	regGrpCnt=StrCnt(pattern, "\\(")
//DbgMsg("regGrpCnt",regGrpCnt)

	if(0!=regGrpCnt)
	{
		// put the string searched to result buffer, line0 all search result
		searchedStr=GetSelectStr(hbuf, searchRst)
		AppendSelect2Buf(hbuf, searchRst, hRstBuf)
		// line for replace
		lnRpl=GetBufLineCount(hRstBuf)
		
		i=1
		while(i<=regGrpCnt)
		{
	 		// form the replace pattern
			replacePat="\\@i@"
//DbgMsg("replacePat", replacePat)

			// line for replace
			AppendSelect2Buf(hbuf, searchRst, hRstBuf)
			//replace the searchedStr to special patten
			ReplaceInBuf(hRstBuf, pattern, replacePat, lnRpl, lnRpl+searchRst.lnLast-searchRst.lnFirst+1, fMatchCase, fRegExp, fWholeWordsOnly, 0)
			
			i=i+1
			lnRpl=lnRpl+1
		}

 		
	}
	
	if("1"==fSel){
		hwnd=GetWndHandle(hbuf)
		if(nil!=hwnd){
			SetWndSel(hwnd, searchRst)
		}
	}
	return searchRst
}


// return the string from the "select record"
macro GetSelectStr(buf, select)
{
	if( (""==select) || (""==buf) ){
		return
	}

	// if the last line only contain a "enter", then ichLim=4096, and the lnLast is line befor the last line
	if(4096==select.ichLim){
//Msg("4096==select.ichLim")
		select.lnLast=select.lnLast+1
		select.ichLim=0
	}

	lines=select.lnLast-select.lnFirst
//DbgMsg("lines", lines)

	// ist line
	strline1=GetBufLine(buf, select.lnFirst)

	// only 1 line
	if(0==lines){
		strSelect=strmid(strline1, select.ichFirst, select.ichLim)
//DbgMsg("strline1st", strSelect)
	}
	else	{
		// the enter asic code ? how to know the enter format dos/unix
		crlf=cat(CharFromAscii (13),CharFromAscii (10))

		// 1st line
		strSelect=strmid(strline1, select.ichFirst, strlen(strline1))
		strSelect=cat(strSelect, crlf)
//DbgMsg("strline1st", strSelect)

		// middle line
		i=1
		while(i<lines){
			strSelect=cat(strSelect, GetBufLine(buf, select.lnFirst+i))
			strSelect=cat(strSelect, crlf)
			i=i+1
		}
		
		// last line
		strlineLst=GetBufLine(buf, select.lnLast)
		strlineLst=strtrunc (strlineLst, select.ichLim)
		strSelect=cat(strSelect, strlineLst)
//DbgMsg("strlineLst", strlineLst)
	}

	return strSelect
}

// return the string from the "select record"
macro AppendSelect2Buf(selBuf, select, putBuf)
{
	if( (""==select) || (""==selBuf) || (""==selBuf) ){
		return
	}

	// if the last line only contain a "enter", then ichLim=4096, and the lnLast is line befor the last line
	if(4096==select.ichLim){
//Msg("4096==select.ichLim")
		select.lnLast=select.lnLast+1
		select.ichLim=0
	}

	lines=select.lnLast-select.lnFirst	
//DbgMsg("lines", lines)

	// ist line
	strline1=GetBufLine(selBuf, select.lnFirst)

	// only 1 line
	if(0==lines){
		strSelect=strmid(strline1, select.ichFirst, select.ichLim)
		AppendBufLine(putBuf, strSelect)
//DbgMsg("strline1st", strSelect)
	}
	else	{
		// 1st line
		strSelect=strmid(strline1, select.ichFirst, strlen(strline1))
		AppendBufLine(putBuf, strSelect)
//DbgMsg("strline1st", strSelect)

		// middle line
		i=1
		while(i<lines){
			AppendBufLine(putBuf, GetBufLine(selBuf, select.lnFirst+i))
			i=i+1
		}
		
		// last line
		strlineLst=GetBufLine(selBuf, select.lnLast)
		strlineLst=strtrunc (strlineLst, select.ichLim)
		AppendBufLine(putBuf, strlineLst)
//gMsg("strlineLst", strlineLst)
	}

	return strSelect
}

macro DbgMsg(str, msg)
{
	out="@str@ @msg@"
	msg(out)
}
